package com.appkubes.mall.restful.application.user.collect.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.core.controller.BaseMallUserCollectController;
import com.appkubes.mall.core.entity.MallUserCollect;
import com.appkubes.mall.restful.application.user.collect.entity.dto.UserCollectUpsertDTO;
import com.appkubes.mall.restful.application.user.collect.entity.vo.UserCollectDelVO;
import com.appkubes.mall.restful.application.user.collect.entity.vo.UserCollectGetVO;
import com.appkubes.mall.restful.application.user.collect.entity.vo.UserCollectUpsertVO;
import com.appkubes.mall.restful.application.user.collect.entity.vo.UserCollectVerifyVO;
import com.appkubes.mall.restful.application.user.collect.service.UserCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/1/12 10:50
 * @Description:
 */

@Slf4j
@Api(tags = "用户收藏功能")
@RestController
@RequestMapping("/user/collect")
public class UserCollectController extends BaseMallUserCollectController {
    @Resource
    UserCollectService userCollectService;

    @ApiOperation("添加与修改收藏信息")
    @PostMapping("/upsert")
    public Response upsert(@RequestBody @ParamValid UserCollectUpsertVO collectUpsertVO) {
        UserCollectUpsertDTO upsertDTO = userCollectService.upsert(collectUpsertVO);
        return Response.success(upsertDTO);
    }

    @ApiOperation("获取收藏信息")
    @GetMapping("/get")
    public Response<MallUserCollect> get(@ParamValid UserCollectGetVO userCollectGetVO) {
        MallUserCollect query = new MallUserCollect();
        BeanUtils.copyProperties(userCollectGetVO, query);
        List<MallUserCollect> userCollects = this.selectListByEntity(query);
        return Response.success(userCollects);
    }

    @ApiOperation("删除收藏信息")
    @GetMapping("/del")
    public Response del(@ParamValid UserCollectDelVO delVO) {
        Boolean effectRow = this.removeByIds(delVO.getCollectKey());
        return Response.success(effectRow);
    }

    @ApiOperation("验证目标Key是否收藏")
    @GetMapping("/verify")
    public Response verify(@ParamValid UserCollectVerifyVO verifyVO) {
        QueryWrapper<MallUserCollect> queryWrapper = new QueryWrapper();
        queryWrapper.in("collect_key", verifyVO.getCollectKey());
        List<MallUserCollect> collectList = this.getBaseMapper().selectList(queryWrapper);
        return Response.success(collectList);
    }

}
